<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            left: 0;
            top: 0;
            width: 200px;
            height: 200px;
            border: 1px solid #000;
        }
    </style>
</head>

<body>
    <div></div>
    <script>
        function animate(obj, target) {
            clearInterval(obj.timer)
            obj.timer = setInterval(function() {
                if (obj.offsetLeft >= target) {
                    //停止动画 本质是停止定时器
                    clearInterval(obj.timer)
                }
                obj.style.left = obj.offsetLeft + 1 + 'px'
            }, 30)
        }
        var div = document.querySelector('div')
        animate(div, 200)
    </script>
</body>

</html>